package com.neusoft.nep.common;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileUtils {
    public static void writeMapObject(String fileName, Map map) {
        try {
            File file = new File(fileName);

            if (!file.exists()) {//如果文件不存在，创建一个文件
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            //对象输出流
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            //oos写入对象
            oos.writeObject(map);
            oos.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Map readMapObject(String fileName)  {
        File file = new File(fileName);
        Map map = null;
        try {
            if (file.length() == 0) {
                map = new HashMap<>();
            } else {
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                map = (Map) ois.readObject();

            }
        } catch (FileNotFoundException e){
            throw new RuntimeException(e);
        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return map;
    }
}
